'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 成长基金
 * @author agl
 */

var async = require('async');
var moment = require('moment');
var Code = require('../../../consts/code');
var moduleType = require('../../../consts/moduleType');
var operationType = require('../../../consts/operationType');
var dataApi = require('../../../libs/dataApi');
var configUtil = require('../../../util/configUtil');
var PlayerManager = require('../../../libs/playerManager');
var activityConf = require('../../../config/activityConf');
var rewardService = require('../../../services/rewardService');
var roleService = require('../../../services/roleService.js');
var constantUtil = require('../../../util/constantUtil');
var activityService = require('../../../services/activityService');
var actType = require('../../../consts/activityType.js');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 购买成长基金
 */
Handler.prototype.buy = function (msg, session, next) {
    var role_id = session.uid;

    var reward_list = [];
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'activity', 'task', 'daily_task', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var activity_model = player_manager.activity;
                var role_model = player_manager.role;

                // 判断VIP等级
                var grow_fund_need_vip_lvl = constantUtil.value('grow_fund_need_vip_lvl');
                if (role_model.vip < grow_fund_need_vip_lvl) {
                    return next(null, {
                        code: Code.USER_VIP_TO_LOW
                    });
                }

                // 扣钻石
                var grow_fund_need_point = constantUtil.value('grow_fund_need_point');
                if (!roleService.subDiamond(player_manager, moduleType.MDL_ACT_GROW_FUND, operationType.OPR_ACT_GROW_FUND_BUY, grow_fund_need_point)) {
                    return next(null, {
                        code: Code.USER_DIAMOND_NOT_ENOUGH
                    });
                }

                // 设置成长基金购买状态
                if (!activity_model.setGrowFundBuyStatus()) {
                    return next(null, {
                        code: Code.ACTIVITY_ALREADY_BOUGHT_GROW_FUND
                    });
                }

                player_manager.save(cb);
                activityService.push(role_id, actType.OPEN, player_manager);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    reward_list: reward_list
                }
            });
        }
    );
};

/**
 * 领取“成长基金”奖励
 */
Handler.prototype.reward = function (msg, session, next) {
    var role_id = session.uid;
    var level = msg.level;

    if (!level) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var reward_list = [];
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'activity', 'bag', 'build', 'soldier', 'task', 'achieve', 'daily_task', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var activity_model = player_manager.activity;
                var role_model = player_manager.role;

                // 判断购买标志
                if (activity_model.grow_fund_buy_status !== 1) {
                    return next(null, {
                        code: Code.ACTIVITY_NOT_BOUGHT_GROW_FUND
                    });
                }

                // 等级不足
                if (role_model.lv < level) {
                    return next(null, {
                        code: Code.USER_LEVEL_NOT_ENOUGH
                    });
                }

                // 取出奖励信息
                var reward_info = activityConf.getGrowFundRewardList(level);
                if (!reward_info) {
                    return next(null, {
                        code: Code.REWARD_ERROR
                    });
                }
                reward_list = [reward_info];

                // 判断是否重复领取
                if (!activity_model.addGrowFundReceiveLevel(level)) {
                    return next(null, {
                        code: Code.ACTIVITY_HAVE_RECEIVE_BEFORE
                    });
                }

                // 发放奖励
                rewardService.addRewardListToRole(player_manager, reward_list, moduleType.MDL_ACT_GROW_FUND, operationType.OPR_ACT_GROW_FUND_REWARD);

                player_manager.save(cb);
                activityService.push(role_id, actType.OPEN, player_manager);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    reward_list: reward_list
                }
            });
        }
    );
};
